﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using Jewelry.Entities;
using System.Globalization;

public partial class Pages_UViewCart : Mgh.UI.Sql.PageBase
{
    #region properties

    protected CultureInfo myInfo;

    private DataSources.Jw_Tbl_CartDataTable tbOrder
    {
        get { return ViewState["order"] as DataSources.Jw_Tbl_CartDataTable; }
        set { ViewState["order"] = value; }
    }

    private DataSources.Jw_Vw_CartDetailDataTable tbDetail
    {
        get { return ViewState["detail"] as DataSources.Jw_Vw_CartDetailDataTable; }
        set
        {
            ViewState["detail"] = value;
            int tt = 0;
            foreach (DataSources.Jw_Vw_CartDetailRow rw in value)
            {
                tt += rw.IsThanhTienNull() ? 0 : rw.ThanhTien;
            }
            Label21.Text = tt.ToString("$#,#", myInfo);
            //Label21.Text = String.Format("0,0", tt);
        }
    }

    #endregion

    #region events

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!CurrentUser.checkUser())
            Response.Redirect("ULogin.aspx?url=" + HttpUtility.UrlEncode(Request.Url.PathAndQuery));
        myInfo = CultureInfo.InvariantCulture;
        if (!IsPostBack)
        {
            this.bindData();
        }
    }

    protected void clickUpdateItem(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow rw = gv.Rows[e.RowIndex];
        string str = (rw.Cells[2].Controls[1] as TextBox).Text;
        int sl = 0;
        if (int.TryParse(str,out sl))
        {
            tbDetail[rw.DataItemIndex].SoLuong = sl < 0 ? 0 : sl;
            tbDetail[rw.DataItemIndex].ThanhTien = sl * tbDetail[rw.DataItemIndex].Gia;


            int tt = 0;
            foreach (DataSources.Jw_Vw_CartDetailRow row in tbDetail)
            {
                tt += row.IsThanhTienNull() ? 0 : row.ThanhTien;
            }
            Label21.Text = tt.ToString("$#,#", myInfo);
        }
        else
        {
            cval.IsValid = false;
            cval.Text = "Chỉ sử dụng kiểu số cho cột số lương!";
        }
    }

    protected void clickUpdate(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            CartBLL bn = new CartBLL(getCurrentConnect());
            if (bn.updateCartDetails(tbDetail))
            {
                bindData();
            }
            else
            {
                cval.IsValid = false;
                cval.Text = bn.getMessage();
            }
        }
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        gv.DataSource = tbDetail;
        gv.DataBind();
    }

    protected void clickPayment(object sender, EventArgs e)
    {
        Response.Redirect("UPayment.aspx");
    }

    #endregion

    #region functions

    private void bindData()
    {
        CartBLL bn = new CartBLL(getCurrentConnect());
        DataSources.Jw_Tbl_CartDataTable tb1 = new DataSources.Jw_Tbl_CartDataTable();
        DataSources.Jw_Vw_CartDetailDataTable tb2 = new DataSources.Jw_Vw_CartDetailDataTable();
        if (bn.getFullCart(CurrentUser.Username, ref tb1, ref tb2))
        {
            tbOrder = tb1;
            tbDetail = tb2;
            if (tbOrder.Count > 0)
            {
                Label1.Text = tbOrder[0].IsNgayTaoNull() ? System.DateTime.Now.ToString("dd/MM/yyyy") : tbOrder[0].NgayTao.ToString("dd/MM/yyyy");
                Label2.Text = tbOrder[0].Username;
            }

            if (tbDetail.Count > 0)
            {
                gv.DataSource = tbDetail;
                gv.DataBind();
            }
        }
        else
        {
            cval.IsValid = false;
            cval.Text = "Xin lỗi quí khác! Hệ thống đang được bảo trì!";
        }

    }

    #endregion
}
